<template>
    <div>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px" class="formBox"
            :rules="rules">

            <el-form-item label="" prop="radioType" label-width="0px">
                <div class="radioTypeTitle">
                    选择数据操作类
                </div>
                <el-radio-group v-model="radioType">
                    <el-radio :label="1" class="radioType_div">
                        信息修改
                        <div class="radioType_div_text">注意： 用于修改用户基本信息：姓名、身份证、手机号等;</div>
                    </el-radio>
                    <el-radio :label="2" class="radioType_div">
                        删除
                        <div class="radioType_div_text">注意：用于顾客信息、病例信息录入错误或者数据录入重复，此条数据无效，即可选择删除，删除之后，可以重新录入;</div>
                    </el-radio>
                    <el-radio :label="3" class="radioType_div">
                        顾客治疗状态回滚
                        <div class="radioType_div_text">注意： 用于顾客治疗信息录入错误，此操作将状态由“ 治疗结束 ” 改成 “ 治疗中/已咨询待诊断/待咨询
                            ”,<br>用户可以修改诊断方案、然后重新记录治疗信息;</div>
                    </el-radio>
                    <el-radio :label="4" class="radioType_div" v-if="deptArray && deptArray.length > 1">
                        就诊门诊修改
                        <div class="radioType_div_text">注意： 用于顾客就诊医院有多个门诊时，登记门诊录入错误，此操作将修改顾客本次就诊门诊;</div>
                    </el-radio>
                </el-radio-group>
            </el-form-item>
            <!-- v-if="radioType == 1 || radioType == 3" -->
            <el-row style="margin-top:20px;">
                <div class="radioTypeTitle">
                    基本信息
                </div>
                <el-form-item label="顾客ID" prop="patientId">
                    <el-input v-model="queryParams.patientId" placeholder="顾客ID" clearable style="margin-bottom:15px"
                        :disabled="true" />
                </el-form-item>
                <el-form-item label="顾客编号" prop="visitNum">
                    <el-input v-model="queryParams.visitNum" placeholder="顾客编号" clearable style="margin-bottom:15px"
                        :disabled="true" />
                </el-form-item>
            </el-row>

            <el-row style="margin-top:20px;" v-if="radioType == 1 || radioType == 3 || radioType == 4">
                <div class="radioTypeTitle">
                    修改前数据
                </div>

                <div v-if="radioType == 1">
                    <el-form-item label="顾客姓名" prop="oldpatientName">
                        <el-input v-model="queryParams.oldpatientName" placeholder="顾客姓名" clearable
                            style="margin-bottom:15px" :disabled="true" />
                    </el-form-item>
                    <el-form-item label="身份证号" prop="oldIdNum">
                        <el-input v-model="queryParams.oldIdNum" placeholder="身份证号" clearable style="margin-bottom:15px"
                            :disabled="true" />
                    </el-form-item>
                    <el-form-item label="手机号" prop="oldpatientPhone">
                        <el-input v-model="queryParams.oldpatientPhone" placeholder="手机号" clearable
                            style="margin-bottom:15px" :disabled="true" />
                    </el-form-item>
                    <el-form-item label="出生日期" prop="oldbirthDate">
                        <el-date-picker v-model="queryParams.oldbirthDate" :disabled="true" placeholder="选择出生日期" style="width: 202px"
                            :picker-options="expireTimeOPtion" type="date" value-format="yyyy-MM-dd">
                        </el-date-picker>
                    </el-form-item>
                </div>
                <div v-if="radioType == 3">
                    <el-form-item label="状态" prop="oldstatus">
                        <el-select v-model="queryParams.oldstatus" filterable placeholder="状态" clearable
                            :disabled="true">
                            <el-option v-for="dict in dict.type.sys_diagnosis_status" :key="dict.value"
                                :label="dict.label" :value="dict.value" />
                        </el-select>
                    </el-form-item>
                </div>
                <div v-if="radioType == 4">
                    <el-form-item label="就诊医院" prop="hospitalId">
                        <el-input v-model="hospitalObj.deptName" placeholder="就诊医院" style="margin-bottom:15px"
                            :disabled="true" />
                    </el-form-item>
                    <el-form-item label="就诊门诊" prop="deptId">
                        <el-select v-model="queryParams.deptId" filterable placeholder="就诊门诊" :disabled="true">
                            <el-option v-for="dict in deptArray" :key="dict.deptId" :label="dict.deptName"
                                :value="dict.deptId" />
                        </el-select>
                    </el-form-item>
                </div>


            </el-row>
            <el-row style="margin-top:20px;" v-if="radioType == 1 || radioType == 3 || radioType == 4">
                <div class="radioTypeTitle">
                    修改后数据（默认显示修改前的数据）
                </div>
                <div v-if="radioType == 1">
                    <el-form-item label="顾客姓名" prop="patientName">
                        <el-input v-model="queryParams.patientName" placeholder="请输入顾客姓名" clearable
                            style="margin-bottom:15px" />
                    </el-form-item>
                    <el-form-item label="身份证号" prop="idNum">
                        <el-input v-model="queryParams.idNum" placeholder="请输入身份证号" clearable
                            style="margin-bottom:15px" />
                    </el-form-item>
                    <el-form-item label="手机号" prop="patientPhone">
                        <el-input v-model="queryParams.patientPhone" placeholder="请输入手机号" clearable
                            style="margin-bottom:15px" />
                    </el-form-item>
                    <el-form-item label="出生日期" prop="birthDate">
                        <el-date-picker v-model="queryParams.birthDate" placeholder="选择出生日期" style="width: 202px"
                            :picker-options="expireTimeOPtion" type="date" value-format="yyyy-MM-dd">
                        </el-date-picker>
                    </el-form-item>
                </div>
                <div v-if="radioType == 3">
                    <el-form-item label="状态" prop="status">
                        <el-select v-model="queryParams.status" filterable placeholder="状态" clearable>
                            <el-option v-for="dict in dict.type.sys_diagnosis_status" :key="dict.value"
                                :label="dict.label" :value="dict.value" />
                        </el-select>

                    </el-form-item>

                </div>
                <div v-if="radioType == 4">
                    <el-form-item label="就诊医院" prop="hospitalId">
                        <el-input v-model="hospitalObj.deptName" placeholder="就诊医院" style="margin-bottom:15px"
                            :disabled="true" />
                    </el-form-item>
                    <el-form-item label="就诊门诊" prop="postMirrorDeptId">
                        <el-select v-model="queryParams.postMirrorDeptId" filterable placeholder="就诊门诊">
                            <el-option v-for="dict in deptArray" :key="dict.deptId" :label="dict.deptName"
                                :value="dict.deptId" />
                        </el-select>
                    </el-form-item>
                </div>


            </el-row>

        </el-form>
        <el-button class="Addbtn" @click="submitEvent">提交申请</el-button>
    </div>
</template>

<script>
//自定义校验规则
var checkIdCard = (rule, value, cb) => {
    const regIdCard =
        /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    if (regIdCard.test(value)) {
        return cb();
    }
    cb(new Error("您输入的身份证号码不是有效格式"));
};
var checkPatientPhone = (rule, value, cb) => {
    const regPatientPhone = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
    if (regPatientPhone.test(value)) {
        return cb();
    }
    cb(new Error("您输入的手机号码不是有效格式"));
};
import { getRecoeryPatientInfo, addRecoveryData } from "@/api/dataRecovery/dataRecovery";
import { mapGetters } from "vuex";
import { listAllDept } from "@/api/system/dept";
export default {
    name: "dataRecovery",
    dicts: ["sys_diagnosis_status"],
    components: {},
    props: ['patientItem'],
    data() {
        return {
            expireTimeOPtion: {
                disabledDate(time) {
                    return time.getTime() > Date.now() - 8.64e6;  //如果没有后面的-8.64e6就是不可以选择今天的
                }
            },
            radioType: 1,
            deptArray: [],
            hospitalObj: {},
            queryParams: {
                oldbirthDate: '',
                birthDate: '',
                oldpatientName: '',
                patientName: "",
                oldIdNum: '',
                idNum: "",
                oldpatientPhone: "",
                patientPhone: '',
                oldpatientId: '',
                status: "",
                oldstatus: '',
                patientId: "",
                visitNum: "",
                deptId: '',
                hospitalId: '',
                postMirrorDeptId: ''
            },
            // 表单校验
            rules: {
                patientName: [
                    {
                        required: true,
                        message: "顾客姓名不能为空",
                    },
                ],
                idNum: [
                    {
                        required: true,
                        message: "身份证号不能为空",
                    },
                    // { validator: checkIdCard, trigger: "blur" },
                ],
                patientPhone: [
                    {
                        required: true,
                        message: "手机号不能为空",
                    },
                    { validator: checkPatientPhone, trigger: "blur" },
                ],
                birthDate:[
                    {
                        required: true,
                        message: "出生日期不能为空",
                    },
                ],
            },

        };
    },
    watch: {},
    created() {

    },
    computed: {

    },
    mounted() {
        console.log("patientItem" + this.patientItem)
        console.log(this.patientItem)
        this.queryParams = {
            oldbirthDate: this.patientItem.birthDate,
            birthDate: this.patientItem.birthDate,
            oldpatientName: this.patientItem.patientName,
            patientName: this.patientItem.patientName,
            oldIdNum: this.patientItem.idNum,
            idNum: this.patientItem.idNum,
            oldpatientPhone: this.patientItem.patientPhone,
            patientPhone: this.patientItem.patientPhone,
            oldpatientId: this.patientItem.patientId,
            status: this.patientItem.status,
            oldstatus: this.patientItem.status,
            patientId: this.patientItem.patientId,
            visitNum: this.patientItem.visitNum,
            deptId: this.patientItem.deptId,
            hospitalId: this.patientItem.hospitalId,
            postMirrorDeptId: this.patientItem.deptId,
        }
        this.getlistAllDept(this.patientItem.hospitalId)
    },

    methods: {
        // 获取所有医院及部门
        getlistAllDept(hospitalId) {
            let that = this
            that.deptArray = []
            listAllDept().then(response => {
                let data = response.data
                //parentId
                data.forEach(function (ele) {
                    if (ele.parentId == hospitalId) {
                        that.deptArray.push(ele)
                    }
                    if (ele.deptId == hospitalId) {
                        that.hospitalObj = ele
                    }
                })
                console.log(that.deptArray)
            });
        },
        submitEvent() {//提交新增
            let that = this
            let params = {
                type: that.radioType,
                postMirrorStatus: that.queryParams.status,
                deptId: that.queryParams.deptId,
                hospitalId: that.queryParams.hospitalId,
                postMirrorIdNum: that.queryParams.idNum,
                patientId: that.queryParams.patientId,
                postMirrorPatientName: that.queryParams.patientName,
                postMirrorPatientPhone: that.queryParams.patientPhone,
                postMirrorBirthDate: that.queryParams.birthDate,
                visitNum: that.patientItem.visitNum,
                postMirrorDeptId: that.queryParams.postMirrorDeptId,
            }
            // console.log(params)
            //  return false
            this.$refs["queryForm"].validate((valid) => {
                if (valid) {
                    var fdStart = params.postMirrorIdNum.indexOf("98");
                    if (fdStart == 0) {

                    } else if (fdStart == -1) {
                        const regIdCard = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
                        if (regIdCard.test(params.postMirrorIdNum)) {

                        } else {
                            this.$message({
                                message: '您输入的身份证号码不是有效格式',
                                type: 'warning'
                            });

                            return false
                        }

                    }
                    addRecoveryData(params).then((response) => {
                        that.imageLoading = false
                        console.log(response)
                        if (response.code == 200) {
                            this.$emit("closeDataRecovery")
                            this.$modal.msgSuccess("提交申请成功，请联系管理员审核~");
                        }

                    });
                }
            })

        }
    }
};
</script>
<style scoped lang="scss">
.header {
    font-weight: bold;
}

.Addbtn {
    background-color: #f3a4a1;
    color: white;
    margin: 0 auto;
    display: block;
}

.radioTypeTitle {
    font-size: 18px;
    font-weight: 700;
    margin-bottom: 10px;
}

.radioType_div {
    font-size: 16px;
    line-height: 25px;
    margin-bottom: 20px;

    ::v-deep .el-radio__label {
        font-size: 16px;
        line-height: 25px;
    }

    ::v-deep .el-radio__inner {
        width: 20px;
        height: 20px;
    }

    &_text {
        font-size: 14px;
        color: red;
        width: 600px;
        word-break: break-all;
    }
}
</style>
